package service

import (
	"gorm.io/gorm"
	"guns-go/global"
	"guns-go/model"
	"guns-go/model/request"
	"guns-go/model/response"
	"guns-go/utils"
	"strconv"
)

func QueryEmpById(empId int) (error, *model.SysEmp) {

	var emp *model.SysEmp
	err := global.GVA_DB.Where("id = ?", empId).Find(&emp).Error

	return err, emp
}

func UpdateSysEmpOrgName(sysEmp *model.SysEmp, tx *gorm.DB) error {
	return tx.Model(model.SysEmp{}).Where("id = ?", sysEmp.Id).Update("org_name", sysEmp.OrgName).Error
}

func QuerySysEmpById(id int, isFillId bool) (err error, e *response.LoginEmpInfo) {

	var emp *model.SysEmp
	err = global.GVA_DB.Where("id = ?", id).Find(&emp).Error

	if err != nil {
		return err, nil
	}

	loginEmpInfo := new(response.LoginEmpInfo)

	if emp != nil {

		loginEmpInfo.JobNum = &(emp.JobNum)
		loginEmpInfo.OrgId = &(emp.OrgId)
		loginEmpInfo.OrgName = &(emp.OrgName)

		//获取附属机构和职位信息
		errEmpExtOrgPosInfo, empExtOrgPosInfo := GetEmpExtOrgPosDictSlice(emp.Id, isFillId)

		if errEmpExtOrgPosInfo != nil {
			return errEmpExtOrgPosInfo, nil
		}

		loginEmpInfo.ExtOrgPos = empExtOrgPosInfo
		//获取所属职位信息
		errEmpPosInfo, empPosInfo := GetEmpPosDictSlice(emp.Id, isFillId)

		if errEmpPosInfo != nil {
			return errEmpPosInfo, nil
		}

		loginEmpInfo.Positions = empPosInfo
	} else {
		loginEmpInfo.ExtOrgPos = make([]map[string]interface{}, 0)
		loginEmpInfo.Positions = make([]map[string]interface{}, 0)
	}

	return err, loginEmpInfo
}

func DeleteEmpInfoByUserId(empId int, tx *gorm.DB) error {

	//删除员工信息
	err := tx.Delete(&model.SysEmp{}, empId).Error

	if err != nil {
		return err
	}

	//级联删除对应的员工-附属信息
	err = DeleteEmpExtInfoByUserId(empId, tx)

	if err != nil {
		return err
	}

	//级联删除对用的员工-职位信息
	err = DeleteEmpPosInfoByUserId(empId, tx)

	if err != nil {
		return err
	}

	return nil
}

func EmpAddOrUpdate(sysEmpParam request.SysEmpParam, tx *gorm.DB) error {

	empId := sysEmpParam.Id

	err, sysEmp := QueryEmpById(empId)

	if err != nil {
		return err
	}

	if sysEmp.Id == 0 {
		sysEmp = new(model.SysEmp)
	}

	err = utils.SimpleCopyProperties(sysEmp, sysEmpParam)

	if err != nil {
		return err
	}

	err = empSaveOrUpdate(sysEmp, tx)

	if err != nil {
		return err
	}

	//编辑附属机构职位相关信息
	extIds := sysEmpParam.ExtIds

	err = ExtOrgPosAddOrEdit(empId, extIds, tx)

	if err != nil {
		return err
	}

	//编辑职位相关信息
	posIds := sysEmpParam.PosIdList

	posIdsInt := make([]int, 0)

	if len(posIds) > 0 {

		for _, id := range posIds {
			posId, _ := strconv.Atoi(id)
			posIdsInt = append(posIdsInt, posId)
		}

	}

	err = EmpPosAddOrEdit(empId, posIdsInt, tx)

	if err != nil {
		return err
	}
	return nil
}

func empSaveOrUpdate(sysEmp *model.SysEmp, tx *gorm.DB) (err error) {

	var emp *model.SysEmp

	//先查询
	err = tx.Where("id = ?", sysEmp.Id).Find(&emp).Error

	if err != nil {
		return err
	}

	if emp.Id != 0 {
		//更新
		err = tx.Model(sysEmp).Updates(sysEmp).Error
	} else {
		//新增
		err = tx.Create(sysEmp).Error
	}

	return err
}

func updateEmpOrgInfo(orgId int, orgName string, tx *gorm.DB) error {

	var sysEmpSlice []*model.SysEmp
	err := global.GVA_DB.Where("org_id = ?", orgId).Find(&sysEmpSlice).Error

	if err != nil {
		return err
	}

	if len(sysEmpSlice) > 0 {

		for _, sysEmp := range sysEmpSlice {
			sysEmp.OrgName = orgName

			err = UpdateSysEmpOrgName(sysEmp, tx)
			if err != nil {
				return err
			}
		}
	}
	return nil
}

func HasOrgEmp(orgId int) (error, bool) {

	var sysEmpSlice []*model.SysEmp
	err := global.GVA_DB.Where("org_id", orgId).Find(&sysEmpSlice).Error

	if err != nil {
		return err, false
	}
	return nil, len(sysEmpSlice) != 0
}
